package com.liangzi.liangzi_common.exception;

import com.liangzi.liangzi_common.result.R;
import com.liangzi.liangzi_common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class UnifiedExceptionhandler {

//    @ExceptionHandler(value = Exception.class)
//    public R handleException(Exception e){
//        log.error(e.getMessage(), e);
//        return R.error();
//    }
//
//    @ExceptionHandler(value = BadSqlGrammarException.class)
//    public R handleException(BadSqlGrammarException e){
//        log.error(e.getMessage(), e);
//        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
//    }
//
//
//    @ExceptionHandler(value = BusinessException.class)
//    public R handleException(BusinessException e){
//        log.error(e.getMessage(), e);
//        return R.error().message(e.getMessage()).code(e.getCode());
//    }
//
//    /**
//     * Controller上一层相关异常
//     */
//    @ExceptionHandler({
//            NoHandlerFoundException.class,
//            HttpRequestMethodNotSupportedException.class,
//            HttpMediaTypeNotSupportedException.class,
//            MissingPathVariableException.class,
//            MissingServletRequestParameterException.class,
//            TypeMismatchException.class,
//            HttpMessageNotReadableException.class,
//            HttpMessageNotWritableException.class,
//            MethodArgumentNotValidException.class,
//            HttpMediaTypeNotAcceptableException.class,
//            ServletRequestBindingException.class,
//            ConversionNotSupportedException.class,
//            MissingServletRequestPartException.class,
//            AsyncRequestTimeoutException.class
//    })
//    public R handleServletException(Exception e) {
//        log.error(e.getMessage(), e);
//        //SERVLET_ERROR(-102, "servlet请求异常"),
//        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
//    }

    @ExceptionHandler(ValidationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handle(ValidationException exception) {
        if(exception instanceof ConstraintViolationException){
            ConstraintViolationException exs = (ConstraintViolationException) exception;

            Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
            for (ConstraintViolation<?> item : violations) {
                //打印验证不通过的信息
                log.info(item.getMessage());
            }
        }
        return R.error().message("请输入合法参数") ;
    }
}
